#include "stdafx.h"
#include "field_writer.h"

namespace irender {

field_writer::field_writer(writer_aptr adaptee, bool upper_first) :
	adaptee_(adaptee),
	upper_first_(upper_first)
{
	buffer_.order(img::bottom_up); //preferable format for img::avi::writer
}

field_writer & field_writer::add(byte_image const & img)
{
	if (buffer_.empty()) {
		buffer_ = img;
	}
	else {
		assert(buffer_.dim() == img.dim());
		unsigned y = upper_first_ ? 1 : 0;
		for (; y < img.height(); y += 2)
			std::copy(img.scan(y), img.scan(y) + img.width(), buffer_.scan(y));
		adaptee_->add(buffer_);
		buffer_.clear();
	}
	return * this;
}

} //namespace irender
